package cn.hg.solon.youcan.system.provider;

import static cn.hg.solon.youcan.system.entity.table.SysOperateLogTableDef.SYS_OPERATE_LOG;

import java.util.Date;
import java.util.Map;

import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.text.StrValidator;
import org.dromara.hutool.core.util.ObjUtil;
import org.noear.solon.annotation.Component;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryOrderBy;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;

import cn.hg.solon.youcan.system.entity.SysOperateLog;
import cn.hg.solon.youcan.system.mapper.SysOperateLogMapper;
import cn.hg.solon.youcan.system.service.SysOperateLogService;

/**
 * @author 胡高
 */
@Component
public class SysOperateLogProvider extends ServiceImpl<SysOperateLogMapper, SysOperateLog>
implements SysOperateLogService {


    /* (non-Javadoc)
     * @see cn.hg.solon.youcan.system.service.SysOperateLogService#pageByMap(com.mybatisflex.core.paginate.Page, java.util.Map)
     */
    @Override
    public Page<SysOperateLog> pageByMap(Page<SysOperateLog> page, Map<String, Object> map) {
        String type = (String)map.get("type");
        String method = (String)map.get("method");
        String status = (String)map.get("status");
        String word = (String)map.get("word");
        Date startDate = (Date)map.get("startDate");
        Date endDate = (Date)map.get("endDate");
        String sortField = (String)map.get("sortField");
        String sortType = (String)map.get("sortType");

        QueryWrapper query = QueryWrapper.create()
            .where(SYS_OPERATE_LOG.TYPE.eq(type).when(StrValidator.isNotBlank(type))
                .and(SYS_OPERATE_LOG.REQUEST_METHOD.eq(method).when(StrValidator.isNotBlank(method)))
                .and(SYS_OPERATE_LOG.STATUS.eq(status).when(StrValidator.isNotBlank(status)))
                .and(SYS_OPERATE_LOG.ACTION_DATETIME.ge(startDate).when(ObjUtil.isNotNull(startDate)))
                .and(SYS_OPERATE_LOG.ACTION_DATETIME.le(endDate).when(ObjUtil.isNotNull(endDate)))
                .and(SYS_OPERATE_LOG.TITLE.like(word).when(StrValidator.isNotBlank(word))
                    .or(SYS_OPERATE_LOG.TYPE.like(word).when(StrValidator.isNotBlank(word)))
                    .or(SYS_OPERATE_LOG.METHOD.like(word).when(StrValidator.isNotBlank(word)))
                    .or(SYS_OPERATE_LOG.USER_NAME.like(word).when(StrValidator.isNotBlank(word)))
                    .or(SYS_OPERATE_LOG.DEPT_NAME.like(word).when(StrValidator.isNotBlank(word)))
                    .or(SYS_OPERATE_LOG.URL.like(word).when(StrValidator.isNotBlank(word)))
                    .or(SYS_OPERATE_LOG.IP.like(word).when(StrValidator.isNotBlank(word)))
                    .or(SYS_OPERATE_LOG.LOCATION.like(word).when(StrValidator.isNotBlank(word)))
                    .or(SYS_OPERATE_LOG.PARAM.like(word).when(StrValidator.isNotBlank(word)))
                    .or(SYS_OPERATE_LOG.RESULT.like(word).when(StrValidator.isNotBlank(word)))
                    .or(SYS_OPERATE_LOG.MESSAGE.like(word).when(StrValidator.isNotBlank(word)))
                )
            ).orderBy(new QueryOrderBy(new QueryColumn(StrUtil.toUnderlineCase(sortField)), sortType));

        return this.page(page, query);
    }

    /* (non-Javadoc)
     * @see cn.hg.solon.youcan.system.service.SysOperateLogService#removeAll()
     */
    @Override
    public boolean removeAll() {
        return this.remove(SYS_OPERATE_LOG.ID.ne(0));
    }

}
